//# sourceURL=http://uMapApp/Module/ToolModule
define([
    "mhjs/mhjs",
    "uMap/uMap",
    "uEarth/uEarth",
    "./Module"
    , "../UI/Panel/FloatPanel"
    , "../UI/Panel/PanelBlock/PanelBlock"
    , "../UI/Panel/Item/FormItem"
    , "../UI/Checkbox"
    , "../UI/SplineDrawer"
], function (
    mhjs,
    uMap,
    uEarth,
    Module
    , FloatPanel
    , PanelBlock
    , FormItem
    , Checkbox
    , SplineDrawer
) {

    function ToolModule(option) {

        var _this = this;

        Module.call(_this, option);

        _this.title = "系统工具";
        _this.icon = _this.app.icons.road;

        _this.panels = [];

        _this.drawer = new SplineDrawer({app: _this.app});
        _this.app.ui.layout.right.append(_this.drawer.buttons);

        _this.measure = new uMap.Measure(_this.app.earth.object);
        _this.measure.close();

        _this.switchPanel = new FloatPanel({"class": "ToolModule switchPanel"});

        _this.switchPanel.drawline = new mhjs.Div({"class": "button"});
        _this.switchPanel.drawline.icon = new mhjs.Img({"class": "icon"});
        _this.switchPanel.drawline.icon.attr("src", _this.app.icons.draw.base64);
        _this.switchPanel.drawline.label = new mhjs.Div({"class": "label"});
        _this.switchPanel.drawline.label.text("线路绘制");

        _this.switchPanel.drawline
            .append(_this.switchPanel.drawline.icon)
            .append(_this.switchPanel.drawline.label)
        ;

        _this.switchPanel.drawline.on("click", function () {
            _this.toggleModule(_this.modules.drawline);
        });

        _this.switchPanel.measureline = new mhjs.Div({"class": "button"});
        _this.switchPanel.measureline.icon = new mhjs.Img({"class": "icon"});
        _this.switchPanel.measureline.icon.attr("src", _this.app.icons.measureline.base64);
        _this.switchPanel.measureline.label = new mhjs.Div({"class": "label"});
        _this.switchPanel.measureline.label.text("距离量测");

        _this.switchPanel.measureline
            .append(_this.switchPanel.measureline.icon)
            .append(_this.switchPanel.measureline.label)
        ;

        _this.switchPanel.measureline.on("click", function () {
            _this.toggleModule(_this.modules.measureline);
        });

        _this.switchPanel.measurearea = new mhjs.Div({"class": "button"});
        _this.switchPanel.measurearea.icon = new mhjs.Img({"class": "icon"});
        _this.switchPanel.measurearea.icon.attr("src", _this.app.icons.measurearea.base64);
        _this.switchPanel.measurearea.label = new mhjs.Div({"class": "label"});
        _this.switchPanel.measurearea.label.text("面积量测");


        _this.switchPanel.measurearea
            .append(_this.switchPanel.measurearea.icon)
            .append(_this.switchPanel.measurearea.label)
        ;

        _this.switchPanel.measurearea.on("click", function () {
            _this.toggleModule(_this.modules.measurearea);
        });

        _this.switchPanel
            .append(_this.switchPanel.drawline)
            .append(_this.switchPanel.measureline)
            .append(_this.switchPanel.measurearea)
        ;

        _this.modules = {};

        _this.modules.drawline = {
            open : function () {
                _this.drawer.open();
            },
            close: function () {
                _this.drawer.close();
            },
            menu : _this.switchPanel.drawline
        };

        _this.modules.measureline = {
            open : function () {
                _this.measure.open("distance");
            },
            close: function () {
                _this.measure.close();
            },
            menu : _this.switchPanel.measureline
        };

        _this.modules.measurearea = {
            open : function () {
                _this.measure.open("square");
            },
            close: function () {
                _this.measure.close();
            },
            menu : _this.switchPanel.measurearea
        };

        _this.app.ui.layout.top.append(_this.switchPanel);


    }

    var _class = ToolModule;

    mhjs.Inherit(_class, Module);

    Object.defineProperties(_class.prototype, {
        "toggleModule": {
            value: function (module) {

                var _this = this;

                if (_this.currentModule !== module) {

                    if (_this.currentModule) {
                        _this.currentModule.close();
                        _this.currentModule.menu.removeClass("active");
                    }

                    _this.currentModule = module;
                    _this.currentModule.open();
                    _this.currentModule.menu.addClass("active");
                }
            }
        },
        "open"        : {
            value: function () {
                var _this = this;
                _this.switchPanel.open(false);
                _this.isOpened = true;
            }
        },
        "close"       : {
            value: function () {
                var _this = this;
                _this.switchPanel.close(false);
                if (_this.currentModule) {
                    _this.currentModule.close();
                    _this.currentModule.menu.removeClass("active");
                }
                _this.isOpened = false;
            }
        },
        "toggle"      : {
            value: function () {
                var _this = this;
                if (_this.isOpened) {
                    _this.close();
                } else {
                    _this.open();
                }
            }
        }
    });

    return _class;

});